package cn.powerone.scm.tasks;

import cn.powerone.common.utils.DateUtils;
import cn.powerone.scm.domain.ScmStatsDaily;
import cn.powerone.scm.domain.ScmStatsMonthly;
import cn.powerone.scm.domain.ScmStatsYearly;
import cn.powerone.scm.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;

@Component("statsTask")
@RequiredArgsConstructor
public class StatsTask {



    private final IScmStatsYearlyService scmStatsYearlyService;
    private final IScmStatsMonthlyService scmStatsMonthlyService;
    private final IScmStatsDailyService scmStatsDailyService;

    private final IScmStatsSupplierMonthlyService scmStatsSupplierMonthlyService;
    private final IScmStatsSupplierYearlyService scmStatsSupplierYearlyService;

    private final IScmProjectsService scmProjectsService;
    private final IScmProductsService scmProductsService;
    private final IScmCustomerService scmCustomerService;
    private final IScmSupplierService scmSupplierService;
    private final IScmCustomerOrdersService scmCustomerOrdersService;
    private final IScmSupplierOrdersService scmSupplierOrdersService;
    private final IScmInventoryLogService scmInventoryLogService;
    public void daily() {
        // 获取昨天的日期
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        Date yesterday = calendar.getTime();

        // 从日期中提取年份和月份
        Calendar yesterdayCalendar = Calendar.getInstance();
        yesterdayCalendar.setTime(yesterday);
        int year = yesterdayCalendar.get(Calendar.YEAR);
        int month = yesterdayCalendar.get(Calendar.MONTH) + 1; // 月份从0开始，需要加1
        int quarter = (month - 1) / 3 + 1;

        // 调用StatsService，更新统计表
        ScmStatsYearly scmStatsYearly = getScmStatsYearly(year);
        ScmStatsMonthly scmStatsMonthly = getScmStatsMonthly(year, month);
        ScmStatsDaily scmStatsDaily = getScmStatsDaily(DateUtils.getNowDate());
        // 更新项目统计信息
        populateProjectStats(yesterday, scmStatsYearly, scmStatsMonthly, scmStatsDaily);
        // 更新产品统计信息
        populateProductStats(yesterday, scmStatsYearly, scmStatsMonthly, scmStatsDaily);
        // 更新供应商统计信息
        populateSupplierStats(yesterday, scmStatsYearly, scmStatsMonthly, scmStatsDaily);
        // 更新客户统计信息
        populateCustomerStats(yesterday, scmStatsYearly, scmStatsMonthly, scmStatsDaily);
        // 更新客户订单统计信息
        populateCustomerOrderStats(yesterday, scmStatsYearly, scmStatsMonthly, scmStatsDaily);
        // 更新供应商订单统计信息
        populateSupplierOrderStats(yesterday, scmStatsYearly, scmStatsMonthly, scmStatsDaily);
        // 更新进出库统计信息
        populateStockStats(yesterday, scmStatsYearly, scmStatsMonthly);

        //持久化数据：
        putScmStatsYearly(scmStatsYearly);
        putScmStatsMonthly(scmStatsMonthly);
        putScmStatsDaily(scmStatsDaily);
    }

    private void populateStockStats(Date yesterday, ScmStatsYearly scmStatsYearly, ScmStatsMonthly scmStatsMonthly) {
        // 调用stock Service，输入昨天的日期， 输出 统计月入库总量
        int monthInCount = scmInventoryLogService.selectChangeQuantityInCountByMonth(yesterday, 1);
        scmStatsMonthly.setTotalStockIn((long) monthInCount);
        // 调用stock Service，输入昨天的日期， 输出 统计年入库总量
        int yearInCount = scmInventoryLogService.selectChangeQuantityInCountByYear(yesterday, 1);
        scmStatsYearly.setTotalStockIn((long) yearInCount);

        // 调用stock Service，输入昨天的日期， 输出 统计月出库总量
         monthInCount = scmInventoryLogService.selectChangeQuantityInCountByMonth(yesterday, 2);
        scmStatsMonthly.setTotalStockOut((long) monthInCount);
        // 调用stock Service，输入昨天的日期， 输出 统计年出库总量
        yearInCount = scmInventoryLogService.selectChangeQuantityInCountByYear(yesterday, 2);
        scmStatsYearly.setTotalStockOut((long) yearInCount);
    }

    private void populateSupplierOrderStats(Date yesterday, ScmStatsYearly scmStatsYearly, ScmStatsMonthly scmStatsMonthly, ScmStatsDaily scmStatsDaily) {
        //调用supplier servcie， 输入昨天的日期， 输出 统计月供应商订单数
        int monthCount = scmSupplierOrdersService.selectSupplierOrderCountByMonth(yesterday);
        scmStatsMonthly.setTotalSupplierOrders((long) monthCount);
        // 调用supplier Service，输入昨天的日期， 输出 统计年供应商订单数
        int yearCount = scmSupplierOrdersService.selectSupplierOrderCountByYear(yesterday);
        scmStatsYearly.setTotalSupplierOrders((long) yearCount);
        // 调用supplier Service，输出 所有供应商订单数
        int totalCount = scmSupplierOrdersService.selectSupplierOrderCountAll();
        scmStatsDaily.setTotalSupplierOrders((long) totalCount);
    }

    private void populateCustomerOrderStats(Date yesterday, ScmStatsYearly scmStatsYearly, ScmStatsMonthly scmStatsMonthly, ScmStatsDaily scmStatsDaily) {
        //调用customer servcie， 输入昨天的日期， 输出 统计月客户订单数
        int monthCount = scmCustomerOrdersService.selectCustomerOrderCountByMonth(yesterday);
        scmStatsMonthly.setTotalCustomerOrders((long) monthCount);
        // 调用customer Service，输入昨天的日期， 输出 统计年客户订单数
        int yearCount = scmCustomerOrdersService.selectCustomerOrderCountByYear(yesterday);
        scmStatsYearly.setTotalCustomerOrders((long) yearCount);
        // 调用customer Service，输出 所有客户订单数
        int totalCount = scmCustomerOrdersService.selectCustomerOrderCountAll();
        scmStatsDaily.setTotalCustomerOrders((long) totalCount);
        // 统计待收款订单 数
        int customerOrdersNotPayed = scmCustomerOrdersService.selectCustomerOrderCountNotPayed();
        scmStatsDaily.setCustomerOrdersNotPayed((long) customerOrdersNotPayed);
        // 统计即将到期的订单 数
        // 获取5天前的日期
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, -5);
        Date fiveDaysAgo = calendar.getTime();
        int customerOrdersExpiring = scmCustomerOrdersService.selectCustomerOrderCountExpiring(fiveDaysAgo);
        scmStatsDaily.setCustomerOrdersExpiring((long) customerOrdersExpiring);
    }

    private void populateCustomerStats(Date yesterday, ScmStatsYearly scmStatsYearly, ScmStatsMonthly scmStatsMonthly, ScmStatsDaily scmStatsDaily) {
        //调用customer servcie， 输入昨天的日期， 输出 统计月创建客户数
        int monthCount = scmCustomerService.selectCustomerCountByMonth(yesterday);
        scmStatsMonthly.setTotalCustomer((long) monthCount);
        // 调用customer Service，输入昨天的日期， 输出 统计年创建客户数
        int yearCount = scmCustomerService.selectCustomerCountByYear(yesterday);
        scmStatsYearly.setTotalCustomer((long) yearCount);
        // 调用customer Service，输出 所有客户数
        int totalCustomer = scmCustomerService.selectCustomerCountAll();
        scmStatsDaily.setTotalCustomer((long) totalCustomer);
    }

    private void populateSupplierStats(Date yesterday, ScmStatsYearly scmStatsYearly, ScmStatsMonthly scmStatsMonthly, ScmStatsDaily scmStatsDaily) {
        //调用supplier servcie， 输入昨天的日期， 输出 统计月创建供应商数
        int monthCount = scmSupplierService.selectSupplierCountByMonth(yesterday);
        scmStatsMonthly.setTotalSupplier((long) monthCount);
        // 调用supplier Service，输入昨天的日期， 输出 统计年创建供应商数
        int yearCount = scmSupplierService.selectSupplierCountByYear(yesterday);
        scmStatsYearly.setTotalSupplier((long) yearCount);
        // 调用supplier Service，输出 所有供应商数
        int totalSupplier = scmSupplierService.selectSupplierCountAll();
        scmStatsDaily.setTotalSupplier((long) totalSupplier);
    }


    private void populateProductStats(Date yesterday, ScmStatsYearly scmStatsYearly, ScmStatsMonthly scmStatsMonthly,ScmStatsDaily scmStatsDaily) {
        //调用products servcie， 输入昨天的日期， 输出 统计月创建项目数
        int monthCount = scmProductsService.selectProductsCountByMonth(yesterday);
        scmStatsMonthly.setTotalProducts((long) monthCount);
        // 调用product Service，输入昨天的日期， 输出 统计年创建项目数
        int yearCount = scmProductsService.selectProductsCountByYear(yesterday);
        scmStatsYearly.setTotalProducts((long) yearCount);
        // 调用product Service，输出 所有项目数
        int totalProducts = scmProductsService.selectProductsCountAll();
        scmStatsDaily.setTotalProducts((long) totalProducts);
    }

    /**
     * 获取统计月信息
     */
    private ScmStatsMonthly getScmStatsMonthly(int year, int month) {
        ScmStatsMonthly scmStatsMonthly = scmStatsMonthlyService.selectScmStatsMonthlyByYearAndMonth(year, month);
        if (scmStatsMonthly == null) {
            scmStatsMonthly = new ScmStatsMonthly();
            scmStatsMonthly.setYear((long) year);
            scmStatsMonthly.setMonth((long) month);
        }
        return scmStatsMonthly;
    }
    /**
     * 持久化数据
     */
    private void putScmStatsMonthly(ScmStatsMonthly scmStatsMonthly) {
        if (scmStatsMonthly.getId() == null){
            scmStatsMonthlyService.insertScmStatsMonthly(scmStatsMonthly);
        }else {
            scmStatsMonthlyService.updateScmStatsMonthly(scmStatsMonthly);
        }
    }

    /**
     * 获取统计年信息
     */
    private ScmStatsYearly getScmStatsYearly(int year) {
        ScmStatsYearly scmStatsYearly = scmStatsYearlyService.selectScmStatsYearlyByYear(year, 0);
        if (scmStatsYearly == null) {
            scmStatsYearly = new ScmStatsYearly();
            scmStatsYearly.setYear((long) year);
            scmStatsYearly.setQuarter(0L);
        }
        return scmStatsYearly;
    }
    /**
     * 持久化数据
     */
    private void putScmStatsYearly(ScmStatsYearly scmStatsYearly) {
        if (scmStatsYearly.getId() == null){
            scmStatsYearlyService.insertScmStatsYearly(scmStatsYearly);
        }else {
            scmStatsYearlyService.updateScmStatsYearly(scmStatsYearly);
        }
    }

    /**
     * 获取统计日信息
     */
    private ScmStatsDaily getScmStatsDaily(Date today) {
        ScmStatsDaily scmStatsDaily = scmStatsDailyService.selectScmStatsDailyByStatsDate(today);
        if (scmStatsDaily == null) {
            scmStatsDaily = new ScmStatsDaily();
            scmStatsDaily.setStatsDate(today);
        }
        return scmStatsDaily;
    }
    /**
     * 持久化数据
     */
    private void putScmStatsDaily(ScmStatsDaily scmStatsDaily) {
        if (scmStatsDaily.getId() == null){
            scmStatsDailyService.insertScmStatsDaily(scmStatsDaily);
        }else {
            scmStatsDailyService.updateScmStatsDaily(scmStatsDaily);
        }
    }

    /**
     * 组装项目统计信息
     */
    private void populateProjectStats(Date yesterday, ScmStatsYearly scmStatsYearly, ScmStatsMonthly scmStatsMonthly,ScmStatsDaily scmStatsDaily) {
        // 调用projectService，输入昨天的日期， 输出 统计月创建项目数
        int monthCount = scmProjectsService.selectProjectsCountByMonth(yesterday);
        scmStatsMonthly.setTotalProjects((long) monthCount);
        System.out.println("统计月创建项目数: " + monthCount);

        // 调用projectService，输入昨天的日期， 输出 统计年创建项目数
        int yearCount = scmProjectsService.selectProjectsCountByYear(yesterday);
        scmStatsYearly.setTotalProjects((long) yearCount);
        System.out.println("统计年创建项目数: " + yearCount);
        // 调用projectService，输出 所有项目数
        int totalProjects = scmProjectsService.selectProjectsCountAll();
        scmStatsDaily.setTotalProjects((long) totalProjects);
        System.out.println("所有项目数: " + totalProjects);
    }
}